package sparrowGui.components.item
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.text.TextField;
	
	import sparrowGui.components.BaseUIComponent;
	import sparrowGui.i.IItem;
	import sparrowGui.utils.SparrowUtil;
	
	/**
	 * 基本项,带有基本的按钮四态，类似于SimplyButton
	 * (此组件并没绑定鼠标事件)
	 * 
	 * 例子如下
	 * 
	 * var itm:SItem = new SListItem();
		itm.update("按钮文字");
		addChild(itm);
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SItem extends BaseUIComponent implements IItem
	{
		static protected const UPSTATE_NAME:String = "upState";
		static protected const OVERSTATE_NAME:String = "overState";
		static protected const DOWNSTATE_NAME:String = "downState";
		static protected const HITTESTSTATE_NAME:String = "hitTestState";
		
		protected var _upState:DisplayObject;
		protected var _overState:DisplayObject;
		protected var _downState:DisplayObject;
		protected var _hitTestState:DisplayObject;
		
		/**
		 * 当前状态
		 */
		private var _currentState:String;
		
		public function SItem(uiVars:Object=null)
		{
			super(uiVars);
			changeState(UPSTATE_NAME);
		}
		
		override protected function setUI(uiVars:Object=null):void
		{
			super.setUI(uiVars);
			
			if(skin is MovieClip && (skin as MovieClip).totalFrames>1)
			{
				changeState(UPSTATE_NAME);
			}
			else if(skin is DisplayObjectContainer)
			{
				var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
//				skinDC.buttonMode = true;
				skinDC.mouseChildren = false;
				
				_upState = skinDC.getChildByName(UPSTATE_NAME);
				_overState = skinDC.getChildByName(OVERSTATE_NAME);
				_downState = skinDC.getChildByName(DOWNSTATE_NAME);
				_hitTestState = skinDC.getChildByName(HITTESTSTATE_NAME);
				
				changeState(UPSTATE_NAME);
			}
			else if(skin is SimpleButton)
			{
				return;
			}
		}
		
		/**
		 * 重新通过新皮肤建立item项
		 * @param argSkin
		 
		public function reset(argSkin:Sprite):void
		{
			this.setUI(argSkin);
			addSkinListen();
		}*/
		
		/**
		 * 更新数据
		 */
		public function update(o:Object):void
		{
			if(skin is SimpleButton)
				SparrowUtil.setButtonText(skin,String(o));
			else if(skin is DisplayObjectContainer)
			{
				var tf:TextField = SparrowUtil.findChild((skin as DisplayObjectContainer)
					,TextField) as TextField;
				if(tf)
					tf.text = String(o);
			}
		}
		
		/**
		 * 用字符改变状态可以解耦，比如在列表中想要添新状态可以继承SItem,SList 
		 * @param stateName
		 */
		public function setState(stateName:String,value:Object=null):void
		{
			if(states.indexOf(stateName)<0) return;
			changeState(stateName);
		}
		
		/**
		 * 改变当前显示状态.
		 * @param stateName
		 */
		protected function changeState(stateName:String):void
		{
			if(skin is MovieClip){
				(skin as MovieClip).gotoAndStop(stateName);
				return;
			}
			
			var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
			if(skinDC){
				hiddenState();
				var state:DisplayObject = skinDC.getChildByName(stateName);
				if(state) state.visible = true;
			}
			this._currentState = stateName;
		}
		
		/**
		 * 可以改变的状态数组
		 * @return 
		 */
		protected function get states():Array
		{
			return [upState,overState,downState]
		}
		
		protected function hiddenState():void
		{
			for each(var dp:DisplayObject in states)
				if(dp) dp.visible = false;
		}
		
		override public function getDefaultUIName():String
		{
			return "item";
		}

		/**
		 * 获取当前项状态
		 * @return 
		 */
		protected function get currentState():String
		{
			return _currentState;
		}

		public function get upState():DisplayObject
		{
			return _upState;
		}

		public function get overState():DisplayObject
		{
			return _overState;
		}

		public function get downState():DisplayObject
		{
			return _downState;
		}

		public function get hitTestState():DisplayObject
		{
			return _hitTestState;
		}


		public function get selected():Boolean
		{
			return false;
		}
		
		public function set selected(value:Boolean):void
		{
		}
		
		public function get itemData():Object
		{
			return null;
		}
	}
}